package com.xysq.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CouponBO;
import com.rockcent.model.Topics;
import com.umeng.analytics.MobclickAgent;
import com.xysq.adapter.ViewPagerAdapter;
import com.xysq.fragment.IntroduceFragment;
import com.xysq.fragment.TicketFragment;
import com.xysq.lemon.R;
import com.xysq.util.PicUtil;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;

import java.util.LinkedList;
import java.util.List;

import butterknife.InjectView;

/**
 * @Author huangdongkai
 */

public class TicketsListActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    @InjectView(R.id.view_pager)
    ViewPager pagerView;
    @InjectView(R.id.txt_current_page)
    TextView currentPageTxt;
    @InjectView(R.id.txt_page_size)
    TextView pageSizeTxt;
    @InjectView(R.id.layout_info)
    LinearLayout infoLayout;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    @InjectView(R.id.layout_loading)
    LinearLayout loadingLayout;
    @InjectView(R.id.layout_reloading)
    LinearLayout reloadingLayout;
    @InjectView(R.id.txt_header_title)
    TextView headerTitleTxt;

    private List<Fragment> fragmentList = new LinkedList<>();
    public static final String BUNDLE_TOPICS = "bundle_topics";
    public static final String TOPICS = "topics";
    public static final String INDEX = "index";
    private Topics topics;
    private Drawable drawable;
    private String backgroundPic;
    private String index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tickets_list);
        initView();
        getData();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra(BUNDLE_TOPICS);
        topics = (Topics) bundle.getSerializable(TOPICS);
        index = bundle.getString(INDEX);
        new Thread(new BitMapThread()).start();
        backIbtn.setOnClickListener(this);
        reloadingLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == backIbtn) {
            finish();
        }
        if (view == reloadingLayout) {
            getData();
        }
    }

    class BitMapThread implements Runnable {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void run() {
            PicUtil picUtil = new PicUtil();
            backgroundPic = topics.getPicUrl();
            backgroundPic = backgroundPic.substring(backgroundPic.indexOf(";") + 1);
            if (!TextUtils.isEmpty(backgroundPic.trim())) {
                Bitmap bitmap = picUtil.returnBitmap(PropertiesUtil.getImageServer() + backgroundPic);
                WindowManager wm = getWindowManager();
                int width = wm.getDefaultDisplay().getWidth();
                int height = wm.getDefaultDisplay().getHeight();
                drawable = picUtil.BlurImages(bitmap, TicketsListActivity.this,width,height);
                Message msg = new Message();
                handler.sendMessage(msg);
            }
        }
    }

    Handler handler = new Handler() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void handleMessage(Message msg) {
            infoLayout.setBackground(drawable);
            loadingLayout.setVisibility(View.GONE);
            super.handleMessage(msg);
        }
    };

    private void initFragment(List<CouponBO> data) {
        IntroduceFragment introduceFragment = new IntroduceFragment(data.size(), topics);
        fragmentList.add(introduceFragment);
        for (CouponBO couponBO : data) {
            TicketFragment ticketFragment = new TicketFragment(couponBO);
            fragmentList.add(ticketFragment);
        }
        ViewPagerAdapter mainPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        pagerView.setAdapter(mainPagerAdapter);
        pagerView.setOnPageChangeListener(this);
        pagerView.setOffscreenPageLimit(fragmentList.size());
    }

    private void getData() {
        appAction.getTopicItemDetail(topics.getUuid(), new CallbackListener<List<CouponBO>>() {
            @Override
            public void onSuccess(List<CouponBO> data) {
                pageSizeTxt.setText(String.valueOf(data.size() + 1));
                initFragment(data);
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(TicketsListActivity.this, message);
                reloadingLayout.setVisibility(View.VISIBLE);
                loadingLayout.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        currentPageTxt.setText(String.valueOf(i + 1));
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("专题浏览页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("专题浏览页");
        MobclickAgent.onPause(this);
    }

    @Override
    public void finish() {
        if(TextUtils.isEmpty(index)) {
            Intent intent = new Intent(this, MainTabActivity.class);
            intent.putExtra(MainTabActivity.INDEX, MainTabActivity.TAG_INDEX);
            startActivity(intent);
        }else {
            super.finish();
        }
    }
}